﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Commons;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Presentation.Materials
{
    public partial class PersonalInfoControl : UserControl
    {
        private readonly Business.Business _business;

        public PersonalInfoControl()
        {
            InitializeComponent();

            _business = MainView.Instance.CreditEducationBusiness;
        }

        private void PersonalInfoControlLoad(object sender, EventArgs e)
        {
            Dock = DockStyle.Fill;

            cboFaculty.DataSource = _business.GetListFacutly();

            cboFaculty.DisplayMember = PropertiesConstants.Name;
            cboFaculty.ValueMember = PropertiesConstants.Id;

            var facultyId = Convert.ToInt32(cboFaculty.SelectedValue);

            if (facultyId <= 0) return;

            cboMajor.DataSource = _business.GetListMajorByIdFaculty(((int)cboFaculty.SelectedValue));

            cboMajor.DisplayMember = PropertiesConstants.Name;
            cboMajor.ValueMember = PropertiesConstants.Id;

            var user = _business.DataContex.Users.FirstOrDefault();

            if (user != null)
            {
                txtName.Text = user.Name;
                txtMoreInformation.Text = user.MoreInformation;
                dtpBirthday.Value = user.Birthday ?? DateTime.Now;

                if (user.Picture != null)
                {
                    picPicture.Image = Utils.ByteArrayToImage(user.Picture);
                }

                cboFaculty.DataBindings.Add("SelectedValue", user, "IdFaculty");
                cboMajor.DataBindings.Add("SelectedValue", user, "IdMajor");
            }
        }

        private void picPicture_DoubleClick(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Filter = "(*.jpg)|*.jpg|(*.bmp)|*.bmp|(*.png)|*.png";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                picPicture.ImageLocation = dialog.FileName;
            }
        }

        private void btnSavePersonalInfo_Click(object sender, EventArgs e)
        {
            var user = new User
                           {
                               Name = txtName.Text.Trim(),
                               MoreInformation = txtMoreInformation.Text.Trim(),
                               Birthday = dtpBirthday.Value,
                               Picture = Utils.ImageToByteArray(picPicture.Image),
                               IdFaculty = ((Faculty)cboFaculty.SelectedItem).Id,
                               IdMajor = ((Major)cboMajor.SelectedItem).Id,
                           };
            _business.AddOrUpdateUser(user);
            MainView.Instance.ApplyChangeUserInformation(user.Name, (Bitmap)picPicture.Image);
        }
    }
}